package com.example.servlet;

import com.example.dao.NovelDao;
import com.example.domain.Novel;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

@WebServlet("/NovelSearchServlet")
public class NovelSearchServlet extends HttpServlet {
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        String keyword = request.getParameter("keyword");
        NovelDao novelDao = new NovelDao();
        List<Novel> novels = novelDao.getSearchCount(keyword);
        if(novels.size()==0){
            request.setAttribute("msg","没有找到相关小说");
            request.getRequestDispatcher("search.jsp").forward(request, response);
            return;
        }
        request.setAttribute("searchNovels",novels);
        request.getRequestDispatcher("search.jsp").forward(request, response);

    }
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request, response);
    }
}
